<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="header">
        <h1 class="title">欢迎使用校园二手交易平台</h1>
      </div>

      <div class="form-wrapper">
        <!-- 用户名输入 -->
        <div class="form-item">
          <label class="form-label">用户名</label>
          <el-input 
            v-model="username"
            placeholder="请输入用户名"
            size="large"
            :prefix-icon="User"
          />
        </div>

        <!-- 密码输入 -->
        <div class="form-item">
          <label class="form-label">密码</label>
          <el-input
            v-model="password"
            type="password"
            placeholder="请输入密码"
            size="large"
            :prefix-icon="Lock"
            show-password
          />
        </div>
      </div>

      <div class="action-buttons">
        <el-button 
          type="primary" 
          size="large" 
          class="login-btn"
          @click="handleLogin"
          :loading="isLoading"
        >
          立即登录
        </el-button>
        <el-button 
          class="register-btn"
          size="large"
          @click="$router.push('/register')"
        >
          注册账号
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import axios from 'axios'
import { ElNotification } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

const username = ref('')
const password = ref('')
const isLoading = ref(false)

const handleLogin = async () => {
  if (!username.value || !password.value) {
    ElNotification({
      title: '错误',
      message: '请输入用户名和密码',
      type: 'error',
    })
    return
  }

  isLoading.value = true

  try {
    const response = await axios.post('http://localhost:3000/users/login', {
      username: username.value,
      password: password.value,
    })
    console.log(response.data)

    if (response.data.user) {
      // 保存登录状态、token 和用户 ID
      localStorage.setItem('token', response.data.token)
      // 修改为获取 user_id
      localStorage.setItem('userId', response.data.user.user_id)
      // 增加用户信息存储
    localStorage.setItem('user', JSON.stringify(response.data.user));
      // 跳转到首页或其他页面
      router.push('/product')
      
      ElNotification({
        title: '成功',
        message: '登录成功',
        type: 'success',
      })
    } else {
      ElNotification({
        title: '错误',
        message: response.data.message || '登录失败',
        type: 'error',
      })
    }
  } catch (error) {
    ElNotification({
      title: '错误',
      message: '服务器错误，请稍后再试',
      type: 'error',
    })
    console.error('登录错误:', error)
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
/* 新增外层容器样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100vw; /* 添加视口宽度 */
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
  box-sizing: border-box; /* 防止内边距影响尺寸 */
}

/* 修改卡片样式 */
.login-card {
  width: 100%; /* 改为百分比宽度 */
  max-width: 600px; /* 设置最大宽度 */
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  position: relative;
  margin: 0 auto; /* 确保居中 */
}
.header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  color: #2c3e50;
  font-size: 24px;
  font-weight: 600;
}

.form-wrapper {
  margin: 30px 0;
}

.form-item {
  margin-bottom: 25px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
}

/* 输入框样式优化 */
:deep(.el-input) {
  --el-input-height: 48px;
  --el-input-border-radius: 8px;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px var(--el-color-primary) inset;
}

.action-buttons {
  display: flex;
  gap: 15px;
  margin-top: 30px;
}

.login-btn {
  flex: 1;
  height: 48px;
  font-size: 16px;
  letter-spacing: 1px;
}

.register-btn {
  flex: 1;
  height: 48px;
  font-size: 16px;
  border: 1px solid var(--el-color-primary);
  color: var(--el-color-primary);
  background: transparent;
}

.register-btn:hover {
  background-color: rgba(64, 158, 255, 0.08);
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-card {
    padding: 30px 20px; /* 减小内边距 */
    width: 95%; /* 在小屏幕上增加宽度占比 */
  }
  
  .title {
    font-size: 20px;
  }
  
  /* 移动端按钮垂直排列 */
  .action-buttons {
    flex-direction: column;
  }
}
</style>